package com.xuanzheng.company.service.impl;

import com.xuanzheng.company.dao.TaskDao;
import com.xuanzheng.company.dao.impl.TaskDaoImpl;
import com.xuanzheng.company.entity.Task;
import com.xuanzheng.company.service.TaskService;
import com.xuanzheng.company.util.IdGenerator;

import java.util.Date;
import java.util.List;

public class TaskServiceImpl implements TaskService {
    private final TaskDao taskDao = new TaskDaoImpl();

    @Override
    public boolean createTask(Task task) {
        // 设置任务ID
        task.setId(IdGenerator.generateId());
        // 设置创建时间
        task.setCreateTime(new Date());
        // 设置初始状态为派遣中(0)
        task.setStatus(0);
        // 设置初始评分为0
        task.setScore(0);
        
        return taskDao.createTask(task);
    }

    @Override
    public Task getTaskById(String id) {
        return taskDao.getTaskById(id);
    }

    @Override
    public List<Task> getTasksByCompanyId(String companyId) {
        return taskDao.getTasksByCompanyId(companyId);
    }

    @Override
    public List<Task> getTasksByEmployeeId(String employeeId) {
        return taskDao.getTasksByEmployeeId(employeeId);
    }

    @Override
    public List<Task> getTasksByManagerId(String managerId) {
        return taskDao.getTasksByManagerId(managerId);
    }

    @Override
    public boolean completeTask(String taskId) {
        // 更新任务完成时间并设置状态为完成待确认(1)
        return taskDao.updateTaskCompleteTime(taskId, new Date());
    }

    @Override
    public boolean confirmTaskCompletion(String taskId, int score) {
        // 确保评分在0-10之间
        if (score < 0) {
            score = 0;
        } else if (score > 10) {
            score = 10;
        }
        
        // 更新任务确认时间、状态为已完成(2)和评分
        return taskDao.confirmTaskCompletion(taskId, score, new Date());
    }

    @Override
    public boolean markTaskAsIncomplete(String taskId) {
        // 标记任务为已完成(2)，但评分为0
        return taskDao.confirmTaskCompletion(taskId, 0, new Date());
    }
    
    @Override
    public boolean deleteTask(String taskId) {
        // 检查任务是否存在
        Task task = taskDao.getTaskById(taskId);
        if (task == null) {
            return false;
        }
        
        // 执行删除操作
        return taskDao.deleteTask(taskId);
    }
    
    @Override
    public boolean updateTaskScore(String taskId, int score) {
        // 确保评分在0-10之间
        if (score < 0) {
            score = 0;
        } else if (score > 10) {
            score = 10;
        }
        
        // 检查任务是否存在且状态为已完成(2)
        Task task = taskDao.getTaskById(taskId);
        if (task == null || task.getStatus() != 2) {
            return false;
        }
        
        // 更新任务评分
        return taskDao.updateTaskScore(taskId, score);
    }
} 